package com.mmlc.meimei.lichengkongjian.classify;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.androidnetframe.httputil.BaseRequestFilterLayer;
import com.androidnetframe.responsejavabean.RequestError;
import com.mmlc.inter.InterActivity;
import com.mmlc.meimei.lichengkongjian.Adapter.HistoryGoodsAdapter;
import com.mmlc.meimei.lichengkongjian.MainActivity;
import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.meimei.lichengkongjian.been.Order;
import com.mmlc.meimei.lichengkongjian.mine.ActivityOrderDetail;
import com.mmlc.network.filter.GetPaymentRequestFilter;
import com.mmlc.network.filter.N1NoParamsRequestFilter;
import com.mmlc.network.responsebean.GetPaymentResponseBean;
import com.mmlc.network.responsebean.HistoryGoodsResponseBean;
import com.mmlc.utils.ViewUtils;
import com.mmlc.widget.MyGridView;

import java.util.List;

/**
 * description: 支付成功展示界面
 * by king
 * Created at 2016/8/31 15:23
 */
public class PayResultActivity extends InterActivity implements View.OnClickListener, AdapterView.OnItemClickListener {

    protected TextView tvPayMode;
    protected TextView tvPayPrice;
    protected TextView tvToInfo;
    protected TextView tvToHome;
    protected TextView tv_integral;
    protected MyGridView gridContinue;
    protected LinearLayout llContinue;
    protected ScrollView scvHome;
    private String order_id;
    private String order_price;
    private String pay_code;
    private String gift_score;

    @Override
    protected int initContentView() {
        return R.layout.activity_pay_result;
    }

    Order order;

    @Override
    protected void initExtra() {
        super.initExtra();
        order = (Order) getIntent().getSerializableExtra("order");
        if (order != null) {
            order_id = order.getOrder_id();
            order_price = order.getOrder_price();
            pay_code = order.getPay_code();
            gift_score = order.getGift_score();
        }
    }

    @Override
    protected void initView() {
        tvPayMode = (TextView) findViewById(R.id.tv_pay_mode);
        tvPayPrice = (TextView) findViewById(R.id.tv_pay_price);
        tvToInfo = (TextView) findViewById(R.id.tv_to_info);
        tvToInfo.setOnClickListener(PayResultActivity.this);
        tvToHome = (TextView) findViewById(R.id.tv_to_home);
        tvToHome.setOnClickListener(PayResultActivity.this);
        tv_integral = (TextView) findViewById(R.id.tv_integral);

        gridContinue = (MyGridView) findViewById(R.id.grid_continue);
        gridContinue.setOnItemClickListener(this);
        llContinue = (LinearLayout) findViewById(R.id.ll_continue);
        scvHome = (ScrollView) findViewById(R.id.scv_home);

        if (!ViewUtils.isEmpty(order_price)) tvPayPrice.setText("¥ " + order_price);
        if(!TextUtils.isEmpty(gift_score)){
            tv_integral.setText(gift_score);
        }
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.tv_to_info) {
            startActivity(new Intent(activity, ActivityOrderDetail.class).putExtra("orderId", order_id));
        } else if (view.getId() == R.id.tv_to_home) {
            startActivity(new Intent(activity, MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK));
        }
        finish();
    }

    @Override
    protected void initInfo() {
        super.initInfo();
        if (!ViewUtils.isEmpty(pay_code))
            getPayment();
        getContinue();
    }

    private void getPayment() {
        GetPaymentRequestFilter filter = new GetPaymentRequestFilter(activity);
        filter.sendRequest(new BaseRequestFilterLayer.AccessResultFromFilter<GetPaymentResponseBean>() {
            @Override
            public void onSuccess(GetPaymentResponseBean result) {
                for (int i = 0; i < result.getData().size(); i++) {
                    GetPaymentResponseBean.DataBean payData = result.getData().get(i);
                    if (!ViewUtils.isEmpty(payData.getPay_code()) && pay_code.equals(payData.getPay_code()))
                        tvPayMode.setText(payData.getPay_name());
                }
            }

            @Override
            public void onFailed(View errorLayout, RequestError serverError, int failedType) {
                if (pay_code.equals("wxpay")) tvPayMode.setText("微信支付");
                else if (pay_code.equals("alipay")) tvPayMode.setText("支付宝支付");
                else if (pay_code.equals("xingwang")) tvPayMode.setText("银行卡支付");
            }
        });
    }

    List<HistoryGoodsResponseBean.DataBean> datas;

    /**
     * 获取继续剁手数据
     */
    private void getContinue() {
        new N1NoParamsRequestFilter(activity, "myHistory")
                .sendRequest(new BaseRequestFilterLayer.AccessResultFromFilter<HistoryGoodsResponseBean>() {
                    @Override
                    public void onSuccess(HistoryGoodsResponseBean result) {
                        if (result != null && result.getData() != null && result.getData().size() > 0) {
                            datas = result.getData();
                            llContinue.setVisibility(View.VISIBLE);
                            gridContinue.setAdapter(new HistoryGoodsAdapter(context, result.getData()));
                        }
                        scvHome.scrollTo(0, 0);
                    }

                    @Override
                    public void onFailed(View errorLayout, RequestError serverError, int failedType) {

                    }
                });
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(activity, ProductInfoActivity.class);
        intent.putExtra("goods_id", datas.get(position).getId());
        startActivity(intent);

        finish();
    }
}
